<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :formType="type" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" v-if="this.type !='show'" type="primary">保 存</el-button>
    </div>

    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchName :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import {
  getDriveCarInfo,
  getDriveWayInfo,
  addDriveManagement,
  editDriveManagement,
  detailDriveManagement,
  queryPotentialById,
  driveCancel,
  idCardIdentify,          // 身份证识别
  driveCardIdentify,       // 驾驶证识别
  isHaveOCR
} from '@/api/potenCus/customer/driveManagement';
import { dictionary } from '@/utils/dictMixin';
import CancelDrive from './cancelDrive';
import SearchName from './searchName';
import { phone, VIN, formatDates, IDCard } from '@/utils/validate';

export default {
  name: 'DriveModal',
  mixins: [dictionary],
  components: {
    SearchName
  },
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    this.toggleList = [
      {
        type: 'UPLOAD_IMG',
        label: '驾驶证正面',
        fieldName: 'drivingLicenceFront',
        valueType: 'Array',
        rules: [{ required: true, message: '请上传图片', trigger: 'change' }],
        change: val => {
          // console.log('val', val)
          console.log('上传回调',this.type, val)
          console.log('车厂是否支持OCR', this.uploadPhotos)
          if(this.type != 'show'){
            // 试驾
            if (this.dataType === this.typeItems[0].value) {
              if(val[0]){
                this.typeItems[0].pics.drivingLicenceFront = val;    //驾驶证
                if(this.uploadPhotos === 10041001){
                  // if(val){
                    driveCardIdentify(val).then(res =>{
                      console.log('驾驶正面识别', res)
                      if(res.resultCode === 200){
                        if(res.data.name){
                          this.formList.find(x => x.fieldName ==='customerName').initialValue = res.data.name
                        }
                        if(res.data.licenseNumber){
                          this.formList.find(x => x.fieldName ==='driverCard').initialValue = res.data.licenseNumber
                        }
                      }
                    })
                  // }
                } else {
                  // 不支持OCR不做操作
                }
              } else {
                if(this.type == 'add'){
                  this.formList.find(x => x.fieldName ==='customerName').initialValue = ''
                  this.formList.find(x => x.fieldName ==='driverCard').initialValue = ''
                }
              }
            }
            // 试乘
            else {
              if(val[0]){
                this.typeItems[1].pics.identityCardFront = val;      //身份证
                if(this.uploadPhotos === 10041001) {
                  idCardIdentify(val).then( res =>{
                    console.log('身份正面识别', res)
                    if(res.resultCode === 200){
                      if(res.data.name){
                        this.formList.find(x => x.fieldName ==='customerName').initialValue = res.data.name
                      }
                      if(res.data.idCardNumber){
                        this.formList.find(x => x.fieldName ==='driverCard').initialValue = res.data.idCardNumber
                      }
                    }
                  })
                } else {
                  // 不支持OCR不做操作
                }
              } else {
                this.formList.find(x => x.fieldName ==='customerName').initialValue = ''
                this.formList.find(x => x.fieldName ==='driverCard').initialValue = ''
              }
            }
          }


        },
        upload: {
          actionUrl: '/api/file/oss/upload',
          fixedSize: [5, 3],
          limit: 1,
          isCalcHeight: true,
        }
      },
      {
        type: 'UPLOAD_IMG',
        label: '驾驶证反面',
        fieldName: 'drivingLicenceBack',
        valueType: 'Array',
        rules: [{ required: true, message: '请上传图片', trigger: 'change' }],
        change: val => {
          console.log('上传回调', val)
            if (this.dataType === this.typeItems[0].value) {
              if(val.length !=0){
                this.typeItems[0].pics.drivingLicenceBack = val;
                // driveCardIdentify(val).then(res =>{
                //   console.log('驾驶背面识别', res)
                // })
              }
            } else {
              if(val.length !=0){
                this.typeItems[1].pics.identityCardBack = val;
                // idCardIdentify(val).then( res =>{
                //   console.log('身份背面识别', res)
                // })
              }
            }
        },
        upload: {
          actionUrl: '/api/file/oss/upload',
          fixedSize: [5, 3],
          limit: 1,
          isCalcHeight: true
        }
      }
    ];
    this.typeItems = [
      {
        value: 20261002,
        text: '试驾',
        pics: {
          drivingLicenceFront: null,
          drivingLicenceBack: null
        }
      },
      {
        value: 20261001,
        text: '试乘',
        pics: {
          identityCardFront: null,
          identityCardBack: null
        }
      }
    ];
    this.dataType = this.typeItems[0].value; // 默认试驾

    this.customerNo = ''; // 潜客编号
    return {
      formList: this.createFormList(),
      handleType: '',
      formData: {},
      visible: false,
      dialogTitle: '',
      conf: { search: '选择试驾客户' },
      appId: localStorage.getItem('appId'),
      uploadPhotos: '',      // 支持/不支持OCR识别
      isAppointment: '',
      isSimple: '',
      testDriveSource: ''
    };
  },
  created() {
    this.getIsHaveOCR();
    this.init();
    this.getDriveCarList();
    this.getDriveWayList();
  },
  methods: {
    // 车厂是否有OCR
    async getIsHaveOCR(){
      const res = await isHaveOCR(this.appId)
      // console.log('让我看看有没有OCR', res)
      if(res.resultCode === 200){
        this.uploadPhotos = res.data.uploadPhotos
      }
    },
    async init() {
      console.log("试驾rowData",this.type, this.appId, this.rowData )
      if (this.type === 'add') {
        this.isAppointment = 10041002
        this.isSimple = 10041002
        this.testDriveSource = 70741001
        this.formList.splice(1, 1);
        this.formList.find(x => x.fieldName === 'employeeName').offsetRightCols = 1;
        // this.formList.find(x => x.fieldName === 'wayId').offsetRightCols = 1;
        if (JSON.stringify(this.rowData) != '{}') {
          this.formList.forEach(x => {
            x.initialValue = x.fieldName === 'driveType' ? x.initialValue : this.rowData[x.fieldName];
          });
        }
          this.customerNo = this.rowData.customerBusinessId; // 潜客编号
        if(this.rowData.customerBusinessId && this.type === 'add' && this.rowData.consultantName){
          this.formList.find(x => x.fieldName === 'employeeName').initialValue = this.rowData.consultantName
        }
      }
      // if ( this.type === 'edit' || this.type ==='show') {
      if ( this.type === 'edit') {
        if(this.rowData.testDriveSource){
          if(this.rowData.testDriveSource === 70741001){
            this.isAppointment = 10041002
            this.isSimple = 10041002
            this.recordId = this.rowData.itemId;
            this.testDriveSource = this.rowData.testDriveSource;
            this.getDriveInfo();
            this.formList.forEach(x => {
              // 说明这个字段得是数组
              if (x.valueType === 'Array') {
                x.initialValue = [this.rowData[x.fieldName]];
              }else
              if (x.fieldName === 'testDriveFileDTOList') {
                if(this.rowData.drivingDisclaimer){
                  x.initialValue = [
                    {
                      name: this.rowData['drivingDisclaimerName'] || '文件',
                      url: this.rowData['drivingDisclaimer']
                    }
                  ];
                }
              } else {
                x.initialValue = this.rowData[x.fieldName];
              }
            });
            this.typeChangeHandle(this.rowData.driveType);
          } else if(this.rowData.testDriveSource === 70741002) {
            this.isAppointment = 10041001
            this.isSimple = 10041002
            this.recordId = this.rowData.itemId;
            this.testDriveSource = this.rowData.testDriveSource;
            this.formList.find(x => x.fieldName ==='driveStatus').hidden = true
            this.formList.find(x => x.fieldName ==='intentCar').hidden = false
            this.formList.find(x => x.fieldName ==='intentCar').disabled = true
            this.formList.find(x => x.fieldName ==='potentialCustomerName').disabled = true
            this.formList.find(x => x.fieldName === 'employeeName').offsetRightCols = 1;
            this.getDriveInfo();
            this.formList.forEach(x => {
              // 说明这个字段得是数组
              if (x.valueType === 'Array') {
                x.initialValue = [this.rowData[x.fieldName]];
              } else
              if (x.fieldName === 'testDriveFileDTOList') {
                if(this.rowData['drivingDisclaimer']){
                  if(this.rowData.drivingDisclaimer !=null){
                    x.initialValue = [
                      {
                        name: this.rowData['drivingDisclaimerName'] || '文件',
                        url: this.rowData['drivingDisclaimer']
                      }
                    ]
                  }
                }
              } else {
                x.initialValue = this.rowData[x.fieldName];
              }
            });
            this.typeChangeHandle(this.rowData.driveType);
          }
        }
      }
      if(this.type ==='show'){
        this.formList.find(x => x.fieldName ==='customerName').rules = []
        this.formList.find(x => x.fieldName ==='driverCard').rules = []
        this.recordId = this.rowData.itemId;
        this.getDriveInfo()
      }
      // 预约试驾
      if(this.type === 'check'){
        this.isAppointment = 10041001
        this.isSimple = 10041002
        this.testDriveSource = 70741002
        this.recordId = this.rowData.itemId;
        this.formList.find(x => x.fieldName ==='driveStatus').hidden = true
        this.formList.find(x => x.fieldName ==='intentCar').hidden = false
        this.formList.find(x => x.fieldName === 'employeeName').offsetRightCols = 1;
        this.getDriveInfo();
        // TODO
        this.formList.forEach(x =>{
          if(x.fieldName === 'customerName'){
            x.initialValue = this.rowData.customerName
          }
          //  else if(x.fieldName === 'testDriveId'){
          //   x.initialValue = this.rowData.carInfoName
          // } else if(x.fieldName === 'wayId'){
          //   x.initialValue = this.rowData.carInfoName
          // }
          else {
            x.initialValue = this.rowData[x.fieldName]
          }
        })
      }
      this.typeChangeHandle(this.rowData.driveType);
      if (this.type === 'show' && this.rowData.driveStatus === 20211002) {
        this.formList.push(
          {
            type: 'INPUT',
            label: '取消原因',
            fieldName: 'cancelExplain',
            placeholder: '',
            initialValue: this.rowData.cancelExplain || this.rowData.cancelCauseRemark,
            style: { width: '200px' },
            disabled: true,
            change: val => {
              this.changeCancel(val);
            }
          },
          {
            type: 'INPUT',
            label: '取消时间',
            fieldName: 'cancelDate',
            placeholder: '',
            initialValue: formatDates(this.rowData.cancelDate),
            style: { width: '200px' }
          }
        );
        if (this.rowData.cancelExplain == '其他') {
          this.formList.push({
            type: 'TEXT_AREA',
            label: '备注',
            fieldName: 'cancelCauseRemark',
            placeholder: '',
            rows: 2,
            style: { width: '200px' }
          });
        }
      }
      this.typeChangeHandle(this.rowData.driveType);
      if (this.type === 'show' && this.rowData.driveStatus === 20211003) {
        this.formList.push(
          {
            type: 'INPUT',
            label: '开始里程',
            fieldName: 'driveSKm',
            placeholder: '开始里程',
            initialValue: this.rowData.driveSKm,
            style: { width: '200px' },
            unitRender: () => {
              return <span>KM</span>;
            },
            disabled: true
          },
          {
            type: 'INPUT',
            label: '结束里程',
            fieldName: 'driveEKm',
            placeholder: '结束里程',
            initialValue: this.rowData.driveEKm,
            style: { width: '200px' },
            unitRender: () => {
              return <span>KM</span>;
            }
          },
          {
            type: 'INPUT',
            label: '本次行驶',
            fieldName: 'driveRunKm',
            placeholder: '本次行驶',
            initialValue: this.rowData.driveRunKm,
            style: { width: '200px' },
            unitRender: () => {
              return <span>KM</span>;
            }
          },
          {
            type: 'INPUT',
            label: '开始时间',
            fieldName: 'driveSAt',
            placeholder: '开始时间',
            initialValue: formatDates(this.rowData.driveSAt),
            style: { width: '200px' }
          },
          {
            type: 'INPUT',
            label: '结束时间',
            fieldName: 'driveEAt',
            placeholder: '结束时间',
            initialValue: formatDates(this.rowData.driveEAt),
            style: { width: '200px' }
          }
        );
      }
    },
    typeChangeHandle(typeCode) {
      // this.$refs.formPanel.SUBMIT_FORM();
      // console.log(this.saveData)
      if (typeCode === this.typeItems[0].value) {
        // this.formList.find(x => x.fieldName ==='customerName').initialValue = ''
        // this.formList.find(x => x.fieldName ==='driverCard').initialValue = ''
        this.typeItems[0].pics.drivingLicenceFront = ''
        this.typeItems[0].pics.drivingLicenceBack = ''
        // this.typeItems[0].pics.drivingLicenceFront = this.rowData['drivingLicenceFront'] ? [this.rowData['drivingLicenceFront']] : this.typeItems[0].pics.drivingLicenceFront;
        // this.typeItems[0].pics.drivingLicenceBack = this.rowData['drivingLicenceBack'] ? [this.rowData['drivingLicenceBack']] : this.typeItems[0].pics.drivingLicenceBack;
        this.typeItems[0].pics.drivingLicenceFront = this.rowData['drivingLicenceFront'] ? [this.rowData['drivingLicenceFront']] : ''
        this.typeItems[0].pics.drivingLicenceBack = this.rowData['drivingLicenceBack'] ? [this.rowData['drivingLicenceBack']] : ''
        this.toggleList[0].label = '驾驶证正面';
        this.toggleList[0].fieldName = 'drivingLicenceFront';
        this.toggleList[1].label = '驾驶证反面';
        this.toggleList[1].fieldName = 'drivingLicenceBack';
        this.toggleList[0].initialValue = this.typeItems[0].pics.drivingLicenceFront;
        this.toggleList[1].initialValue = this.typeItems[0].pics.drivingLicenceBack;
      }
      if (typeCode === this.typeItems[1].value) {
        // this.formList.find(x => x.fieldName ==='customerName').initialValue = ''
        // this.formList.find(x => x.fieldName ==='driverCard').initialValue = ''
        this.typeItems[1].pics.identityCardFront = ''
        this.typeItems[1].pics.identityCardBack = ''
        // this.typeItems[1].pics.identityCardFront = this.rowData['identityCardFront'] ? [this.rowData['identityCardFront']] : this.typeItems[1].pics.identityCardFront;
        // this.typeItems[1].pics.identityCardBack = this.rowData['identityCardBack'] ? [this.rowData['identityCardBack']] : this.typeItems[1].pics.identityCardBack;
        this.typeItems[1].pics.identityCardFront = this.rowData['identityCardFront'] ? [this.rowData['identityCardFront']] : ''
        this.typeItems[1].pics.identityCardBack = this.rowData['identityCardBack'] ? [this.rowData['identityCardBack']] : ''
        this.toggleList[0].label = '身份证正面';
        this.toggleList[0].fieldName = 'identityCardFront';
        this.toggleList[1].label = '身份证反面';
        this.toggleList[1].fieldName = 'identityCardBack';
        this.toggleList[0].initialValue = this.typeItems[1].pics.identityCardFront;
        this.toggleList[1].initialValue = this.typeItems[1].pics.identityCardBack;
      }
    },
    createFormList() {
      return [
        {
          type: 'RADIO',
          label: '试驾类型',
          fieldName: 'driveType',
          selfCols: 2,
          placeholder: '请选择',
          initialValue: this.typeItems[0].value,
          itemList: this.typeItems,
          change: val => {
            this.dataType = val;
            this.typeChangeHandle(val);
          },
          rules: [{ required: true, message: '请选择试驾类型', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '试驾状态',
          fieldName: 'driveStatus',
          placeholder: '请选择',
          hidden: false,
          itemList: this.createDictList('2021'),
          disabled: this.type == 'edit' ? true : false
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'potentialCustomerName',
          placeholder: '点击搜索试驾客户',
          initialValue: '',
          readonly: true,
          disabled: this.type === 'check' ? true : false,
          unitRender: () => {
            return (
              <el-button
                slot="append"
                disabled={JSON.stringify(this.rowData) != '{}' && this.type === 'add'}
                icon="el-icon-search"
                onClick={JSON.stringify(this.rowData) != '{}' && this.type === 'add' ? '' : this.openDriveCancel}
              ></el-button>
            );
          },
          rules: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '客户手机号',
          fieldName: 'potentialCustomerPhone',
          placeholder: '',
          initialValue: '',
          maxlength: 11,
          readonly: true,
          rules: [
            { required: true, message: '客户手机号不能为空', trigger: 'blur' },
            { trigger: 'blur', validator: phone }
          ]
        },
        {
          type: 'INPUT',
          label: '销售顾问',
          fieldName: 'employeeName',
          placeholder: '',
          readonly: true,
          rules: [{ required: true, message: '销售顾问不能为空', trigger: 'blur' }]
        },
        ...this.toggleList,
        {
          type: 'INPUT',
          label: '试驾人名称',
          fieldName: 'customerName',
          placeholder: '请输入',
          initialValue: '',
          rules: [{ required: true, message: '试驾人名称不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '试驾人手机号',
          fieldName: 'mobile',
          placeholder: '请输入',
          initialValue: '',
          maxlength: 11,
          rules: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { trigger: 'blur', validator: phone }
          ]
        },
        {
          type: 'INPUT',
          label: '证件号码',
          fieldName: 'driverCard',
          placeholder: '请输入',
          rules: [
            { required: true, message: '证件号码不能为空', trigger: 'blur' },
            { trigger: 'blur', validator: IDCard }
          ]
        },
        {
          type: 'INPUT',
          label: '意向试驾车型',
          fieldName: 'intentCar',
          placeholder: '',
          hidden: true,
          disabled: this.type === 'check' ? true : false,
        },
        {
          type: 'SELECT',
          label: '试驾车辆',
          fieldName: 'testDriveId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '试驾车辆不能为空', trigger: 'blur' }],
          change: val => {
            this.changeVinSelect(val);
          }
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          disabled: true,
          placeholder: '请输入',
          initialValue: '',
          rules: [{ required: true, message: 'VIN不能为空', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '路线选择',
          fieldName: 'wayId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '路线不能为空', trigger: 'blur' }]
        },
        {
          type: 'UPLOAD_FILE',
          label: '试乘试驾责任书',
          selfCols: 2,
          style: { width: '200px' },
          fieldName: 'testDriveFileDTOList',
          placeholder: '试乘试驾责任书...',
          disabled: this.type == 'show' ? true : false,
          rules: [{ required: true, message: '请上传试乘试驾责任书', trigger: 'change' }],
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 1
          }
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = _.cloneDeep(val);
    },
    // 获取试驾明细
    async getDriveInfo() {
      const res = await detailDriveManagement(this.recordId);
      console.log('试驾明细', res, this.type);
      if (res.resultCode === 200) {
        this.formList.forEach(x => {
          if (x.fieldName === 'vin') {
            x.initialValue = res.data[x.fieldName];
          } else if (x.fieldName === 'wayId') {
            let isHasWayId = x.itemList.some(item => item.value === res.data['wayId']);
            !isHasWayId ? x.itemList.push({ text: res.data['wayName'], value: res.data['wayId'] }) : '';
          } else if (x.fieldName === 'testDriveId') {
            let isTestDriveId = x.itemList.some(item => item.value === res.data['testDriveId']);
            !isTestDriveId ? x.itemList.push({ text: res.data['carInfoName'], value: res.data['testDriveId'] }) : '';
          } else if(x.fieldName === 'customerName') {
            x.initialValue = res.data[x.fieldName];
          } else if(x.fieldName === 'driverCard') {
            x.initialValue = res.data[x.fieldName];
          } else if(x.fieldName ==='testDriveFileDTOList'){
            x.initialValue = [
              {
                name: res.data['drivingDisclaimerName'] || '文件',
                url: res.data['drivingDisclaimer'] || ''
              }
            ];
          } else if (x.valueType === 'Array') {
            x.initialValue = [res.data[x.fieldName]];
          }
          else {
            x.initialValue = res.data[x.fieldName];
          }
          if(this.type ==='show'){
            if (x.fieldName === 'testDriveId'){
              x.initialValue = res.data.carInfoName;
            } else if(x.fieldName === 'wayId'){
              x.initialValue = res.data.wayName;
            }
          }
        });
      }
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        if (this.dataType === this.typeItems[0].value) {
          // 试驾
          if (Array.isArray(this.saveData.drivingLicenceFront) && this.saveData.drivingLicenceFront.length) {
            this.saveData.drivingLicenceFront = this.saveData.drivingLicenceFront[0];
          }
          if (Array.isArray(this.saveData.drivingLicenceBack) && this.saveData.drivingLicenceBack.length) {
            this.saveData.drivingLicenceBack = this.saveData.drivingLicenceBack[0];
          }
          delete this.saveData.identityCardFront;
          delete this.saveData.identityCardBack;
        }
        if (this.dataType === this.typeItems[1].value) {
          // 试乘
          if (Array.isArray(this.saveData.identityCardFront) && this.saveData.identityCardFront.length) {
            this.saveData.identityCardFront = this.saveData.identityCardFront[0];
          }
          if (Array.isArray(this.saveData.identityCardBack) && this.saveData.identityCardBack.length) {
            this.saveData.identityCardBack = this.saveData.identityCardBack[0];
          }
          delete this.saveData.drivingLicenceFront;
          delete this.saveData.drivingLicenceBack;
        }
        let res = {};
        console.log('rowData',this.rowData)
        console.log('saveData',this.saveData)
        this.saveData.isAppointment = this.isAppointment
        this.saveData.isSimple = this.isSimple
        this.saveData.testDriveSource = this.testDriveSource
        if (this.type === 'add') {
          this.saveData.testDriveSource = 70741001;
          this.saveData.customerNo = this.customerNo;
          if(this.rowData.customerBusinessId && this.rowData.consultant){
            this.saveData.employeeName = this.rowData.consultant
          }
          res = await addDriveManagement(this.saveData);
        }
        if (this.type === 'edit') {
          this.saveData.employeeName = this.rowData.consultant
          this.saveData.customerNo = this.customerNo;
          res = await editDriveManagement(this.recordId, this.saveData);
        }
        if (this.type === 'check') {
          this.saveData.employeeName = this.rowData.consultant
          this.saveData.customerNo = this.customerNo;
          this.saveData.testDriveSource = 70741002;
          res = await editDriveManagement(this.recordId, this.saveData);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    resetFormList(data) {
      this.formList[0].initialValue = this.formList[0].initialValue == '20261002' ? this.typeItems[1].value : this.typeItems[0].value;
      const keys = Object.keys(data);
      this.formList.forEach(x => {
        if (keys.includes(x.fieldName)) {
          x.initialValue = data[x.fieldName];
        }
      });
      this.customerNo = data.customerNo;
    },
    closeHandler(val, data) {
      this.formList[0].initialValue = this.formList[0].initialValue == '20261002' ? this.typeItems[1].value : this.typeItems[0].value;
      this.visible = val;
      if (!data) return;
      this.resetFormList(data);
    },
    async getDriveCarList() {
      const res = await getDriveCarInfo();
      // console.log('试驾车型', res);
      if (res.resultCode === 200) {
        let driveCarList = res.data.map(x => ({ value: x.testDriveId, text: x.carInfoName }));
        this.formList.find(x => x.fieldName === 'testDriveId').itemList.push(...driveCarList);
        // this.formList.find(x => x.fieldName === 'testDriveId').itemList =driveCarList
      }
    },
    async changeVinSelect(testDriveId) {
      const res = await getDriveCarInfo(testDriveId);
      if (res.resultCode === 200) {
        var self = res.data;
        self.map((item, index) => {
          if (item.testDriveId == testDriveId) {
            // this.formList.vin = Number(item.vin)
            this.formList.vin = item.vin;
            console.log('试驾vin', this.formList.vin);
            this.formList.forEach(x => {
              if (x.fieldName == 'vin') {
                x.initialValue = this.formList.vin;
              }
            });
          }
        });
      }
    },
    async getDriveWayList() {
      const res = await getDriveWayInfo();
      if (res.resultCode === 200) {
        let drivewaylist = res.data.map(x => ({ value: x.wayId, text: x.wayName }));
        // let oldList = [...this.formList.find(x => x.fieldName === 'wayId').itemList]
        // let newDrivewaylist = drivewaylist.filter(item => {
        //   return oldList[0].value !== item.value
        // })
        this.formList.find(x => x.fieldName === 'wayId').itemList.push(...drivewaylist);
        // this.formList.find(x => x.fieldName === 'wayId').itemList = drivewaylist;
      }
    },
    async openDriveCancel() {
      this.handleType = 'search';
      this.visible = true;
      this.showHandler();
    }
  }
};
</script>

<style lang="less" scoped></style>
